package com.demo.Thread_;

/**
 * 演示多线程执行案例
 *
 * @author gy
 * @date 2022/4/21
 */
public class Thread03 {

    public static void main(String[] args) {
        System.out.println("main 方法线程名字为：" + Thread.currentThread().getName());
        T1 t1 = new T1();
        T2 t2 = new T2();
        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t2);
        thread1.start();
        thread2.start();
        for (int i = 0; i < 30; i++) {
            System.out.println("main 线程正在执行" + (i + 1));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}

class T1 implements Runnable {
    int count;

    @Override
    public void run() {
        // 在此处写自己的业务代码 ，让Thread-0 工作
        while (true) {
            System.out.println("T1线程名字为：" + Thread.currentThread().getName());
            System.out.println("hello world" + count++);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 60) {
                break;
            }
        }
    }
}

class T2 implements Runnable {
    int count;

    @Override
    public void run() {
        // 在此处写自己的业务代码 ，让Thread-1 工作
        while (true) {
            System.out.println("T2线程名字为：" + Thread.currentThread().getName());
            System.out.println("hi " + count++);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (count == 40) {
                break;
            }
        }
    }
}